package org.example.utils;

import com.alibaba.fastjson2.JSON;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
import org.example.factory.messagequeue.UserFactory;
import org.example.model.messagequeue.User;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author huangy
 * @description
 * @since 2023/2/10 17:17
 **/
@Component
public class ElasticSearchUtils {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    public void add() throws IOException {
        User user = UserFactory.createUser();
        IndexRequest indexRequest = new IndexRequest("user").id(String.valueOf(user.getId()));
        String userJson = JSON.toJSONString(user);
        indexRequest.source(userJson, XContentType.JSON);
        restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    }

}
